//
//  MTabBarController.swift
//  MM2
//
//  Created by 李成明 on 2021/12/31.
//

import UIKit
import SnapKit

private class MTabBarItem: UIView {

    private lazy var icon: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        return view
    }()
    
    private var norImg: UIImage
    private var selectImg: UIImage
    
    init(frame: CGRect, norImg: UIImage, selectImg: UIImage) {
        self.norImg = norImg
        self.selectImg = selectImg
        
        super.init(frame: frame)
        
        addSubview(icon)
        icon.image = norImg
        icon.snp.makeConstraints { make in
            make.center.equalTo(self)
            make.size.equalTo(CGSize(width: 30, height: 30))
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickAction))
        self.addGestureRecognizer(tap)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var isSelect = false {
        didSet {
            icon.image = isSelect ? selectImg : norImg
        }
    }
    
    private var clickClosure: ((MTabBarItem) -> Void)?
    
    @objc private func clickAction() {
        clickClosure?(self)
    }
    
    @discardableResult
    func click(_ closure: @escaping (MTabBarItem) -> Void) -> MTabBarItem {
        clickClosure = closure
        return self
    }
}

private class MTabBarView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var faceAnaView: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "pk_img")
        view.isUserInteractionEnabled = true
        view.contentMode = .scaleAspectFit
        
        view.image = UIImage(named: "tabbar_heart_icon")
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(pkClick))
        view.addGestureRecognizer(tap)
        
        return view
    }()
    
    private var clickClosure:((Int) -> Void)?
    private var currentView: MTabBarItem?
    
    private func setupUI() {
        backgroundColor = .white
        let itemW = kScreenWidth/3
        let itemH = ktabBarHeight
        var x = 0.0
        let item1 = MTabBarItem.init(frame: CGRect(x: x, y: 0, width: itemW, height: itemH), norImg:  UIImage(named: "tabbar_homepage_icon")!, selectImg: UIImage(named: "tabbar_homepage_select")!)
        item1.click { [weak self] view in
            view.isSelect = true
            self?.currentView?.isSelect = false
            self?.currentView = view
            self?.faceAnaView.image = UIImage(named: "tabbar_heart_icon")
            self?.clickClosure?(0)
        }.isSelect = true
        currentView = item1
        x += itemW
        
        faceAnaView.frame = CGRect(x: x + itemW/2 - 35, y: -20, width: 70, height: 70)
        x += itemW
        
        let item2 = MTabBarItem.init(frame: CGRect(x: x, y: 0, width: itemW, height: itemH),  norImg:  UIImage(named: "tabbar_mine_icon")!, selectImg: UIImage(named: "tabbar_mine_icon_select")!)
        item2.click { [weak self] view in
            view.isSelect = true
            self?.currentView?.isSelect = false
            self?.currentView = view
            self?.faceAnaView.image = UIImage(named: "tabbar_heart_icon")
            self?.clickClosure?(2)
        }
        
        addsubViews([item1, item2, faceAnaView])
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let supView = super.hitTest(point, with: event)
        for subView in self.subviews {
            //将父类point点转换成子类坐标系下的点
            let convertedPoint = subView.convert(point, from: self)
            let targetView = subView.hitTest(convertedPoint, with: event)
            if targetView != nil {
                return targetView
            }
        }
        return supView
    }
    
    @discardableResult
    func click(_ closure: @escaping (Int) -> Void) -> MTabBarView {
        clickClosure = closure
        return self
    }
    
    @objc private func pkClick() {
        currentView?.isSelect = false
        currentView = nil
        faceAnaView.image = UIImage(named: "tabbar_heart_select")
        clickClosure?(1)
    }
}

class MTabBarController: UITabBarController {
    
    static func turnTotabbar() {
        guard let window = UIApplication.shared.keyWindow else { return }
        window.rootViewController = MTabBarController()
    }
    
    func hidentabar() {
        UIView.animate(withDuration: 0.25) {
            self.mTabBarView.frame = CGRect(x: 0, y: kScreenHeight + 20, width: kScreenWidth, height: kTabBarAndSafeH)
        }
    }
    
    func showTababr() {
        UIView.animate(withDuration: 0.25) {
            self.mTabBarView.frame = CGRect(x: 0, y: kScreenHeight - kTabBarAndSafeH, width: kScreenWidth, height: kTabBarAndSafeH)
        }
    }
    
    private lazy var mTabBarView: MTabBarView = {
        let view = MTabBarView(frame: CGRect(x: 0, y: kScreenHeight - kTabBarAndSafeH, width: kScreenWidth, height: kTabBarAndSafeH))
        view.click { [weak self] index in
            self?.selectedIndex = index
        }
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let frontPageNav = MNavigationController.init(rootViewController: FrontPageVC())
        self.addChild(frontPageNav)
        
        let scoreNav = MNavigationController.init(rootViewController: ScoreVC())
        self.addChild(scoreNav)
        
        let mineNav = MNavigationController.init(rootViewController: MineVC())
        self.addChild(mineNav)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        
        if #available(iOS 11.0, *) {
            let safeAreaFrame = self.view.safeAreaLayoutGuide.layoutFrame
            kStateBarHeight = safeAreaFrame.origin.y
            kSafeAreaBottomH = kScreenHeight - safeAreaFrame.size.height - kStateBarHeight
            kStateAndNavH = kStateBarHeight + kNaviBarHeight
            kTabBarAndSafeH = ktabBarHeight + kSafeAreaBottomH
            kSafeUseHeight = kScreenHeight - kStateAndNavH - kTabBarAndSafeH
        }
        
        setupUI()
    }
    
    private func setupUI() {
        tabBar.isHidden = true
        view.addSubview(mTabBarView)
    }
}
